import {Component} from '@angular/core';
import {IMapCreator} from './imap-creator';
import {MapCreateOption} from "./models/map-create-option";
import {MapConfig} from './map-config';
import {MapType} from "./models/map-type";
import {BMapCreator} from "./map-baidu/map-creator";
import {GeolocateType} from "./models/geolocate-type";
import {EventHubService} from "../../services/event-hub.service";
import {IMapBusiness} from "./imap-business";
import {MapBusinessFactory} from "./map-business-factory";
import {MapApi} from "../../model/map/MapApi";
import {AdpMapCommon} from "./adp-map-common";

@Component({
    selector: 'adp-map',
    templateUrl: 'adp-map.html'
})
export class AdpMap {
    options: MapCreateOption;
    public creator: IMapCreator;
    mapContainerId: string;
    mapBusiness: IMapBusiness;
    mapApi: MapApi;
    eventHubKey: string = "adp-map";
    public adpMapCommon: AdpMapCommon;
    isHideGeoTool: number = 0;

    constructor() {
    }

    create(lat: number, lng: number, wrapperId: string, geoType: GeolocateType, isHideGeoTool = -1) {
        this.options = new MapCreateOption();
        this.options.mapType = MapConfig.mapType;
        this.creator = AdpMap.getMapCreator(MapConfig.mapType);
        this.isHideGeoTool = isHideGeoTool;

        let self = this;
        //地图初始化完成
        EventHubService.on("map-ready", this.eventHubKey, (options) => {
            self.mapApi = options.mapApi;
        });

        //地图定位完成
        EventHubService.on("map-geolocated", this.eventHubKey, (options) => {
            if (options.geolocatedType === GeolocateType.UserLocation) {
                self.options.userLat = options.center.lat;
                self.options.userLng = options.center.lng;
            }
        });

        this.options.lat = lat;
        this.options.lng = lng;
        this.mapContainerId = `mapWrapper${new Date().getTime()}`;//this.options.wrapperId = wrapperId
        this.options.geolocatedType = geoType;
        this.options.wrapperId = this.mapContainerId;
        this.mapBusiness = MapBusinessFactory.getMapBusiness();
        this.adpMapCommon = new AdpMapCommon();

        setTimeout(() => {
            this.creator.createMap(this.options);
        }, 500);
    }

    static getMapCreator(type: MapType): IMapCreator {
        let creator: IMapCreator;
        if (type === MapType.BaiduMap) {
            creator = new BMapCreator();
        } else {
            creator = new BMapCreator();
        }
        return creator;
    }

    geolocationClick() {
        if (!this.options.userLat) {
            // if(this.options.geolocatedType !== GeolocateType.UserLocation){
            this.adpMapCommon.getUserLatLng({
                mapApi: this.mapApi,
                geolocatedType: GeolocateType.UserLocation,
                center: this.options,
            });
        } else {
            let point = {
                lat: this.options.lat,
                lng: this.options.lng
            };
            // this.mapBusiness.panTo(point, this.mapApi);
            EventHubService.emit("map-go-user-location", {
                center: point,
                mapApi: this.mapApi,
                geolocatedType: GeolocateType.UserLocation
            });
        }

    }

}
